package com.liveneo.system.base.utils;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

import com.liveneo.adapter.base.entity.PrpmUser;
import com.liveneo.adapter.base.support.SpringContextHolder;
import com.liveneo.system.base.security.Principal;
import com.liveneo.system.base.utils.CacheUtil;
import com.liveneo.system.base.utils.EncryptUtil;
import com.liveneo.system.base.utils.MD5;
import com.liveneo.system.service.UserService;

/**
 * 用户信息帮助类
 */
public class UserUtil {

    private static UserService userService = SpringContextHolder.getBean(UserService.class);

    /** 加密需要的参数 */
    public static final int HASH_ITERATIONS = 1024;
    public static final int SALT_SIZE = 8;

    public static final String CACHE_ROLE_LIST = "user_role_list";
    public static final String CACHE_MENU_LIST = "user_menu_list";

    public static final String USER_CACHE = "userCache";
    public static final String USER_CACHE_ID = "id_";
    public static final String USER_CACHE_LOGINNAME = "loginName_";

    /**
     * 获取当前登陆的用户信息
     * 
     * @return
     */
    public static Principal getPrincipal() {
        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();
        return principal;
    }

    /**
     * 获取当前登陆的用户对象
     * 
     * @return 如果登陆的用户为空，则返回空User
     */
    public static PrpmUser getCurrentUser() {
        Principal principal = getPrincipal();
        if (principal != null) {
            PrpmUser user;
            try {
                user = userService.queryById(principal.getId());
                if (user != null) {
                    return user;
                } else {
                    return new PrpmUser();
                }
            } catch (Exception e) {
                e.printStackTrace();
                return new PrpmUser();
            }

        } else {
            return new PrpmUser();
        }
    }

    /**
     * 获得用户的会话信息
     * 
     * @return
     */
    public static Session getSession() {
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        return session;
    }

    /**
     * 修改用户密码
     * 
     * @param user
     * @return
     */
    public boolean updateUserPasword(PrpmUser user) {
        CacheUtil.remove(USER_CACHE, USER_CACHE_ID + user.getId());
        CacheUtil.remove(USER_CACHE, USER_CACHE_LOGINNAME + user.getUserName());
        // return mapper.updateUserPasword(user) > 0;
        return false;
    }

    /**
     * 修改用户登陆的相关信息
     * 
     * @param user
     * @return
     */
    public boolean updateUserLonginInfo(PrpmUser user) {
        CacheUtil.remove(USER_CACHE, USER_CACHE_ID + user.getId());
        CacheUtil.remove(USER_CACHE, USER_CACHE_LOGINNAME + user.getUserName());
        // return mapper.updateUserLonginInfo(user) > 0;
        return false;
    }

    /**
     * 修改用户信息
     * 
     * @param user
     * @return
     */
    public boolean updateUserInfo(PrpmUser user) {
        // user.preUpadate();
        CacheUtil.remove(USER_CACHE, USER_CACHE_ID + user.getId());
        CacheUtil.remove(USER_CACHE, USER_CACHE_LOGINNAME + user.getUserName());
        // return mapper.update(user) > 0;
        return false;
    }

    /**
     * 对密码进行加密 SHA-1
     * 
     * @param plainPassword 明文密码
     * @return
     */
    public static String enctypePassword(String plainPassword) {
        byte[] salt = EncryptUtil.generateSalt(SALT_SIZE);
        byte[] hashPass = EncryptUtil.sha1(plainPassword.getBytes(), salt, HASH_ITERATIONS);
        return EncryptUtil.encodeHex(salt) + EncryptUtil.encodeHex(hashPass);

    }

    /**
     * 校验密码是否有效
     * 
     * @param plainPass 明文密码
     * @param password 密文密码
     * @return
     */
    public static boolean validatePassword(String plainPass, String password) {
        // byte[] salt = EncryptUtil.decodeHex(password.substring(0, 16));
        // byte[] hashPass = EncryptUtil.sha1(plainPass.getBytes(), salt, HASH_ITERATIONS);
        // return password.equals(EncryptUtil.encodeHex(salt) + EncryptUtil.encodeHex(hashPass));
        return plainPass.equals(password);

    }

    /**
     * 对密码进行加密MD5和 SHA-1
     * 
     * @param plainPassword 明文密码
     * @return
     */
    public static String encPassword(String plainPassword) {
        return enctypePassword(MD5.GetMD5Code(plainPassword));

    }

    // --------------------------用户会话信息的缓存操作-----------------------//
    public static void putCache(String key, Object value) {
        getSession().setAttribute(key, value);
    }

    public static Object getCache(String key) {
        return getSession().getAttribute(key);
    }

    public static void removeCache(String key) {
        getSession().removeAttribute(key);
    }
}
